<template>
  <div class="table-box">
    <!-- 搜索表单 -->
    <el-form :model="searchForm" ref="searchFormRef" :inline="true" class="search-form">
      <el-form-item label="产品编码" prop="productCode">
        <el-input v-model="searchForm.productCode" placeholder="请输入产品编码" clearable />
      </el-form-item>
      <el-form-item label="产品名称" prop="productName">
        <el-input v-model="searchForm.productName" placeholder="请输入产品名称" clearable />
      </el-form-item>
      <el-form-item label="产品类别" prop="category">
        <el-select v-model="searchForm.category" placeholder="请选择类别" clearable style="width: 180px">
          <el-option label="电子产品" value="ELECTRONICS" />
          <el-option label="机械产品" value="MACHINERY" />
          <el-option label="化工产品" value="CHEMICAL" />
        </el-select>
      </el-form-item>
      <el-form-item label="库存状态" prop="status">
        <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 180px">
          <el-option label="正常" value="NORMAL" />
          <el-option label="预警" value="WARNING" />
          <el-option label="缺货" value="OUT_OF_STOCK" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-header">
      <el-button type="primary" :icon="CirclePlus" @click="handleAdd">成品入库</el-button>
      <el-button type="warning" :icon="Minus" @click="handleOutbound">成品出库</el-button>
      <el-button type="success" :icon="Download" @click="handleExport">导出</el-button>
    </div>

    <el-table :data="tableData" style="width: 100%" v-loading="loading" border>
      <el-table-column type="index" label="序号" width="60" />
      <el-table-column prop="productCode" label="产品编码" />
      <el-table-column prop="productName" label="产品名称" />
      <el-table-column prop="specification" label="规格型号" />
      <el-table-column prop="category" label="产品类别">
        <template #default="scope">
          <el-tag>{{ getCategoryText(scope.row.category) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="unit" label="单位" />
      <el-table-column prop="currentStock" label="当前库存" align="right">
        <template #default="scope">
          <span :class="getStockClass(scope.row)">{{ scope.row.currentStock }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="reservedStock" label="预留库存" align="right" />
      <el-table-column prop="availableStock" label="可用库存" align="right">
        <template #default="scope">
          <span class="available-stock">{{ scope.row.currentStock - scope.row.reservedStock }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="unitPrice" label="单价" align="right">
        <template #default="scope">
          ¥{{ scope.row.unitPrice }}
        </template>
      </el-table-column>
      <el-table-column prop="totalValue" label="库存总值" align="right">
        <template #default="scope">
          <span class="total-value">¥{{ (scope.row.currentStock * scope.row.unitPrice).toFixed(2) }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="库存状态">
        <template #default="scope">
          <el-tag :type="getStatusType(scope.row.status)">{{ getStatusText(scope.row.status) }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="lastUpdateTime" label="最后更新时间" />
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="scope">
          <el-button type="primary" link @click="handleView(scope.row)">查看详情</el-button>
          <el-button type="warning" link @click="handleMove(scope.row)">库存转移</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        :page-sizes="[10, 20, 50, 100]"
        :total="pagination.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes, prev, pager, next, jumper"
      />
    </div>
  </div>
</template>

<script setup lang="ts" name="finishedProductInventory">
import { ref, reactive, onMounted } from "vue";
import type { FormInstance } from "element-plus";
import { ElMessage } from "element-plus";
import { CirclePlus, Download, Minus } from "@element-plus/icons-vue";

const loading = ref(false);
const tableData = ref([]);
const searchFormRef = ref<FormInstance>();

// 查询表单
const searchForm = reactive({
  productCode: "",
  productName: "",
  category: "",
  status: ""
});

// 分页配置
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
});

// 获取类别文本
const getCategoryText = (category: string) => {
  const categoryMap: Record<string, string> = {
    ELECTRONICS: "电子产品",
    MACHINERY: "机械产品",
    CHEMICAL: "化工产品"
  };
  return categoryMap[category] || category;
};

// 获取状态类型
const getStatusType = (status: string) => {
  const statusMap: Record<string, string> = {
    NORMAL: "success",
    WARNING: "warning",
    OUT_OF_STOCK: "danger"
  };
  return statusMap[status] || "info";
};

// 获取状态文本
const getStatusText = (status: string) => {
  const statusMap: Record<string, string> = {
    NORMAL: "正常",
    WARNING: "预警",
    OUT_OF_STOCK: "缺货"
  };
  return statusMap[status] || status;
};

// 获取库存样式
const getStockClass = (row: any) => {
  if (row.currentStock <= 0) return "stock-danger";
  if (row.currentStock <= 50) return "stock-warning";
  return "stock-normal";
};

// 获取表格数据
const getTableData = async () => {
  loading.value = true;
  try {
    // 模拟数据
    const mockData = [
      {
        id: 1,
        productCode: "FP001",
        productName: "智能手机X1",
        specification: "6.1寸 128GB",
        category: "ELECTRONICS",
        unit: "台",
        currentStock: 150,
        reservedStock: 20,
        unitPrice: 2999.00,
        status: "NORMAL",
        lastUpdateTime: "2024-01-15 14:30:00"
      },
      {
        id: 2,
        productCode: "FP002",
        productName: "工业机器人A1",
        specification: "6轴 负载10kg",
        category: "MACHINERY",
        unit: "台",
        currentStock: 8,
        reservedStock: 2,
        unitPrice: 89999.00,
        status: "WARNING",
        lastUpdateTime: "2024-01-15 13:15:00"
      }
    ];
    
    tableData.value = mockData;
    pagination.total = mockData.length;
  } catch (error) {
    console.error("获取成品库存失败:", error);
    ElMessage.error("获取成品库存失败");
  } finally {
    loading.value = false;
  }
};

// 查询
const handleSearch = () => {
  pagination.currentPage = 1;
  getTableData();
};

// 重置
const handleReset = () => {
  if (!searchFormRef.value) return;
  searchFormRef.value.resetFields();
  pagination.currentPage = 1;
  getTableData();
};

// 分页大小改变
const handleSizeChange = (val: number) => {
  pagination.pageSize = val;
  getTableData();
};

// 当前页改变
const handleCurrentChange = (val: number) => {
  pagination.currentPage = val;
  getTableData();
};

// 成品入库
const handleAdd = () => {
  ElMessage.info("成品入库功能开发中");
};

// 成品出库
const handleOutbound = () => {
  ElMessage.info("成品出库功能开发中");
};

// 导出
const handleExport = () => {
  ElMessage.info("导出功能开发中");
};

// 查看详情
const handleView = (row: any) => {
  ElMessage.info(`查看 ${row.productName} 详情`);
};

// 库存转移
const handleMove = (row: any) => {
  ElMessage.info(`转移 ${row.productName} 库存`);
};

// 页面加载时获取数据
onMounted(() => {
  getTableData();
});
</script>

<style scoped>
.search-form {
  background-color: #fff;
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 16px;
}

.table-header {
  margin-bottom: 16px;
  display: flex;
  gap: 10px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
  flex-shrink: 0;
}

.stock-normal {
  color: #67c23a;
  font-weight: bold;
}

.stock-warning {
  color: #e6a23c;
  font-weight: bold;
}

.stock-danger {
  color: #f56c6c;
  font-weight: bold;
}

.available-stock {
  color: #409eff;
  font-weight: bold;
}

.total-value {
  color: #67c23a;
  font-weight: bold;
}
</style>